from direct.gui.DirectGui import *
from pandac.PandaModules import NodePath
from direct.interval.IntervalGlobal import *
from panda3d.core import Vec2,Point3,Vec3,Vec4
from pandac.PandaModules import TextNode
'''
	Andrew Oberlin
	Date: February 29, 2012
'''
class MenuButton:
	def __init__(self, parent, name, texturePath, command, pos, numFrames = 10, scale=0.5,fps=5,
			textureExt='png', isMovie = True, isInterval = False, text=None, textPos=None,
			textScale=None, extraArgs=[]):
		self.parent = parent
		self.command = command
		self.texturePath = texturePath
		self.textureExt = textureExt
		self.isMovie = isMovie
		self.isInterval = isInterval
		self.numFrames = numFrames
		self.name = name
		self.pos = pos
		self.text = text
		self.textPos = textPos
		self.textScale = textScale
		self.scale = scale
		self.fps = fps
		self.extraArgs = extraArgs
		self.overTextColor = (199.0/255, 241.0/255, 117/255.0, 1)
		self.outTextColor = (0.3, 1, 0.3, 1)
		self.loadModel()
		
	def loadModel(self):
		self.buttonDummy = self.parent.base.aspect2d.attachNewNode(self.name + '_button_dummy')
		self.dummy = NodePath(self.name + '_dummy')
		self.model = self.parent.loader.loadModelCopy("models/plane")
		self.model.setTransparency(1)
		if (self.isMovie):
			self.modelTextures = self.loadTextureMovie(self.numFrames, self.texturePath, self.textureExt)
		else:
			self.modelTextures = [self.parent.loader.loadTexture(self.texturePath + '.' + self.textureExt)]
		
		self.model.setTexture(self.modelTextures[0])
		self.model.reparentTo(self.dummy)
		
		self.button = DirectButton(geom=self.dummy, scale=self.scale,
			command=self.command, relief = None, pressEffect = 0, 
			clickSound = self.parent.soundLoader.music['click'], extraArgs=self.extraArgs)
		self.button.reparentTo(self.buttonDummy)	
		self.buttonDummy.setPos(self.pos)
		self.button.bind(DGG.WITHIN, self.mouseOver) 
		self.button.bind(DGG.WITHOUT, self.mouseOut)

		if (self.isMovie):
			task = self.parent.taskMgr.add(self.animate, 'animate_' + self.name)
			task.fps = self.fps
		elif (self.isInterval):
			interval = self.button.hprInterval(4, Vec3(0,0,-360))
			interval.loop()
			
		if (self.text != None):
			self.textNode = TextNode(self.name + '_text')
			self.textNode.setText(self.text)
			self.textModel = self.parent.base.aspect2d.attachNewNode(self.textNode)
			if (self.textScale == None):
				self.textModel.setScale(Vec3(0.10,0.10,0.075))
			else:
				self.textModel.setScale(self.textScale)
			self.textModel.reparentTo(self.buttonDummy)
			self.textModel.setColor(self.outTextColor)
			if (self.textPos != None):
				self.textModel.setPos(self.textPos)
			else:
				self.textModel.setPos(-0.05, 0, .25)
		
	'''
		Our custom load function to load the textures needed for a movie into a
		list. It assumes the the files are named
		"path/name<serial number>.extention"
		
		Params: frames -- The number of frames to load
				name -- The "path/name" part of the filename path
				suffix -- The "extention" part of the path
				padding -- The number of digit the serial number contians:
					e.g. if the serial number is 0001 then padding is 4
	'''
	def loadTextureMovie(self, frames, name, suffix, padding = 1):
		return [self.parent.loader.loadTexture((name+"_%0"+str(padding)+"d."+suffix) % i) for i in range(frames)]
		

	def mouseOver(self, event):
		if (self.text != None):
			self.textModel.setColor(self.overTextColor)
			
	def mouseOut(self, event):
		if (self.text != None):
			self.textModel.setColor(self.outTextColor)

	def animate(self, task):
		currentFrame = int(task.time * task.fps)
		self.button.setTexture(self.modelTextures[currentFrame % len(self.modelTextures)], 1)
		
		return task.cont
		
	def hide(self):
		self.buttonDummy.hide()
		
	def show(self):
		self.buttonDummy.show()
		
	def removeNode(self):
		self.buttonDummy.removeNode()
		
	def reparentTo(self, dummy):
		self.buttonDummy.reparentTo(dummy)